#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>

#include "storage.h"



static List* mem_vector;
static size_t next_free;
static char* obarray;
static char* next_symbol;

#define REF(x) (mem_vector + (size_t)POINTER(x))

int list_prepare()
{
    mem_vector = (List*)malloc( VECTOR_SIZE );
    obarray = (char*)malloc( VECTOR_SIZE );
    next_free = 0;
    next_symbol = obarray;
    return mem_vector && obarray;
}

void list_close()
{
    free( mem_vector );
    putchar('\n');
    /*
    int i = 0;
    for( i = 0; i < 10 ; i++ )
    {
        putchar( *(obarray+i) );
    }
    putchar('\n');
    free( obarray );
    */
}

Indx list_car( Indx l )
{
    return REF(l)->Car;
}

Indx list_cdr( Indx l )
{
    return REF(l)->Cdr;
}

void list_set_car( Indx l, Indx car )
{
    REF(l)->Car = car;
}

void list_set_cdr( Indx l, Indx cdr)
{
    REF(l)->Cdr = cdr;
}

Indx list_cons( Indx car, Indx cdr )
{
    list_set_car( next_free, car );
    list_set_cdr( next_free, cdr );
    return next_free++;
}

int list_eq( Indx l, Indx r)
{
    return l == r;
}

Indx obarray_append( char* src, size_t len )
{
    char* p = next_symbol;
    while ( next_symbol - p < len )
    {
        *next_symbol = toupper( *src );
        next_symbol++;
        src++;
    }
    REF(next_free)->Car = (Indx)p;
    REF(next_free)->Cdr = len;
    return next_free++;
}

List* obarray_get( Indx p )
{
    return REF(p);
}

